package com.mitcub.panda.controller;

import com.mitcub.panda.base.BaseController;
import com.mitcub.panda.entity.Member;
import com.mitcub.panda.query.AdminQuery;
import com.mitcub.panda.service.IMemberService;
import com.mitcub.panda.utils.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @项目名称: panda
 * @类名称: MemberController
 * @类描述: 注册会员控制器
 * @作者: tangloong
 * @创建时间: 2021/11/9 14:45
 */
@Controller
@RequestMapping("/member/member")
public class MemberController extends BaseController {
    @Autowired
    IMemberService memberService;

    @ResponseBody
    @RequestMapping("/list")
    public RespResult list(AdminQuery query) {
        return memberService.getList(query);
    }

    /**
     * 编辑 新增 记录详情
     * @param id    记录ID
     * @param model 模型
     * @return
     */
    @Override
    public String edit(Long id, Model model) {
        Map<String, Object> info = new HashMap<>();
        if (id != null && id > 0) {
            info = memberService.info(id);
        }
        model.addAttribute("info", info);
        return super.edit(id, model);
    }

    /**
     * 添加记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/add")
    public RespResult add(@RequestBody Member entity) {
        return memberService.edit(entity);
    }

    /**
     * 修改记录
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/update")
    public RespResult update(@RequestBody Member entity) {
        return memberService.edit(entity);
    }



    /**
     * 删除记录
     * @param id 记录ID
     * @return
     */
    @ResponseBody
    @GetMapping("/delete/{id}")
    public RespResult delete(@PathVariable("id") Long id) {
        return memberService.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids 记录ID(多个使用逗号","分隔)
     * @return
     */
    @ResponseBody
    @GetMapping("/batchDelete/{ids}")
    public RespResult batchDelete(@PathVariable("ids") String ids) {
        return memberService.deleteByIds(ids);
    }

    /**
     * 设置状态
     * @param entity 实体对象
     * @return
     */
    @ResponseBody
    @PostMapping("/setStatus")
    public RespResult setStatus(@RequestBody Member entity) {
        Member member = new Member();
        member.setId(entity.getId());
        member.setStatus(entity.getStatus());
        return memberService.setStatus(member);
    }

}
